﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";

    var GetPicDomin = System.Configuration.ConfigurationManager.AppSettings["Pic_Domin"];
}
<script src="~/Content/js/dialog/dialog.js"></script>
<script src="~/Content/js/common.js"></script>
<script src="~/Content/js/ajaxfileupload.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/lang/zh-cn/zh-cn.js"></script>
<style>
    #editor {
        overflow: scroll;
        max-height: 300px
    }
</style>
<form id="form1">
    <input type="hidden" id="Details" name="Details" />
    <div style="margin-top: 10px; margin-left: 10px; margin-right: 10px;">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#">基本信息</a></li>
        </ul>
        <div style="padding-top: 20px; margin-right: 30px;">
            <table class="form">
                <tr>
                    <th class="formTitle">标题</th>
                    <td class="formValue" colspan="3">
                        <input id="Title" name="Title" type="text" class="form-control required" placeholder="活动标题"  maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">活动描述</th>
                    <td class="formValue" colspan="3">
                        <textarea id="Description" name="Description" class="form-control" style="height: 60px;" maxlength="100"></textarea>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">图片</th>
                    <td class="formValue" colspan="2">
                        <img id="img1" width="380" height="190" />
                        <input type="file" id="file1" name="file1" style="display:none;" onchange="ajaxFileUpload();" />
                    </td>
                    <td>
                        <button type="button" class="btn btn-success" style="margin-right: 50px;float:right;" onclick="UpdateSinglePic();">上传封面图片</button>
                        <input type="hidden" id="Pic" name="Pic">
                    </td>

                </tr>
                <tr>
                    <th class="formTitle">详细信息</th>
                    <td class="formValue" colspan="3">
                        <textarea id="txtEditorContents" name="txtEditorContents" style="height:400px;width:900px;"></textarea>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
<script src="~/Content/js/wysiwyg/bootstrap-wysiwyg.js"></script>
<script src="~/Content/js/wysiwyg/external/jquery.hotkeys.js"></script>
<style src="~/Content/js/wysiwyg/index.css"></style>

<script>
    function UpdateSinglePic() {
        $("#file1").click();
    }
</script>
<script type="text/javascript">
    var GetPicDomin = "@GetPicDomin";
    var keyValue = $.request("keyValue");
    function ajaxFileUpload() {
        $.ajaxFileUpload
            (
            {
                url: '/PicUpload/UploadNewsPic', //用于文件上传的服务器端请求地址
                secureuri: false, //是否需要安全协议，一般设置为false
                fileElementId: 'file1', //文件上传域的ID
                dataType: 'json', //返回值类型 一般设置为json
                success: function (data, status)  //服务器成功响应处理函数
                {
                    $("#img1").attr("src", GetPicDomin + data.imgurl);
                    $("#Pic").val(data.imgurl);
                    if (typeof (data.error) != 'undefined') {
                        if (data.error != '') {
                            alert(data.error);
                        } else {
                            alert(data.msg);
                        }
                    }
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                }
            }
            )
        return false;
    }

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('txtEditorContents');

    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        $("#Details").val(ue.getContent()); //.html()
        $.submitForm({
            url: "/Activity/SubmitForm?keyValue=" + keyValue,
            param: $("#form1").formSerialize(),
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

    $(function () {
        if (!!keyValue) {
            $.ajax({
                url: "/Activity/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#img1").attr("src", GetPicDomin + data.Pic);
                    $("#Pic").val(data.Pic);
                    $("#form1").formSerialize(data);
                    
                    ue.addListener("ready", function () {
                        ue.setContent(data.Details);
                    });  
                }
            });
        }
    });
</script>  